
Public Class FormDepartmentAED
    Public gId As Int16
    Dim myGroup As New GroupInfo
    Private Sub FormDepartmentAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        txtDepartment.Text = ""
    End Sub
    Private Sub SaveUpdate()
        Try
            'Passing values from object in class to TextBox
            With myGroup
                .GroupId = gId
                .GroupName = txtDepartment.Text
                .Active = 1

                If gId = Nothing Then
                    'Insert Department

                    Groups.AddDepartment(myGroup)

                Else
                    'Update Department

                    Groups.UpdateDepartment(myGroup)

                End If

            End With
            AddDepartmentToDataGridview()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Close()
    End Sub
    Private Sub AddDepartmentToDataGridview()
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If
            'Passing Values DataGridView into form Department
            With FormDepartments.dgvDepartment.CurrentRow
                .Cells("GROUP_ID").Value = gId
                .Cells("GROUP_NAME").Value = txtDepartment.Text
                If gId = Nothing Then
                    .Cells("GROUP_ID").Value = myGroup.GroupId
                    RefreshData.DescendingRefresh(FormDepartments.dgvDepartment, 0)
                    FormDepartments.dgvDepartment.CurrentCell = FormDepartments.dgvDepartment.Rows(0).Cells(1)
                Else
                    FormDepartments.dgvDepartment.CurrentCell = FormDepartments.dgvDepartment.Rows(0).Cells(1)

                    Exit Sub
                End If

            End With
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click
        SaveUpdate()
    End Sub
End Class